package com.elepro.shoppingmall.service.finance;


import com.elepro.shoppingmall.entity.finance.Finance;
import com.elepro.shoppingmall.entity.finance.SplitDate;
import com.elepro.shoppingmall.entity.finance.YMD;
import com.elepro.shoppingmall.mapper.finance.FinanceMapper;
import com.elepro.shoppingmall.utils.Finance.FinanceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class FinanceServiceIm implements FinanceService{

    @Autowired
    private FinanceMapper financeMapper;

    /**
     * 求今天的销售额情况
     * @return
     */
    @Override
    public SplitDate getFinanceListByToDay( Date toDay , Boolean a) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(toDay);

        String[] toDaySplit = FinanceUtils.getDateStringUtils(toDay);
        List<Finance> financeList = financeMapper.getFinanceListByToDay(toDaySplit[0],toDaySplit[1],toDaySplit[2]);

        List<String> name = new ArrayList<>();
        List<Double> money = new ArrayList<>();
        Double moneys = new Double(0);
        for (Finance finance : financeList){
            name.add(finance.getHms());
            money.add(finance.getAllMoney());
            moneys +=finance.getAllMoney();
        }
        if (a){
            SplitDate splitDate = new SplitDate("今天的销售情况",moneys,name,money);
            return splitDate;
        }

        SplitDate splitDate = new SplitDate(format+"的销售情况",moneys,name,money);
        return splitDate;
    }

    @Override
    public SplitDate getFinanceListToYear() {
        System.out.println("year");
        List<YMD> ymdList = financeMapper.getFinanceListByYear();
        System.out.println(ymdList.toString());
        List<String> name = new ArrayList<>();
        List<Double> money = new ArrayList<>();
        Double moneys = new Double(0);
        for (YMD ymd : ymdList){
            name.add(ymd.getName());
            money.add(ymd.getMoney());
            moneys += ymd.getMoney();
        }


        return new SplitDate("每年的销售情况",moneys,name,money);
    }

    @Override
    public SplitDate getFinanceListToMonth() {

        List<YMD> ymdList = financeMapper.getFinanceListByMonth();

        List<String> name = new ArrayList<>();
        List<Double> money = new ArrayList<>();
        Double moneys = new Double(0);
        for (YMD ymd : ymdList){
            name.add(ymd.getName()+"-"+ymd.getName1());
            money.add(ymd.getMoney());
            moneys += ymd.getMoney();
        }


        return new SplitDate("每月的销售情况",moneys,name,money);
    }

    @Override
    public SplitDate getFinanceListToDay() {

        List<YMD> ymdList = financeMapper.getFinanceListByDay();

        List<String> name = new ArrayList<>();
        List<Double> money = new ArrayList<>();
        Double moneys = new Double(0);
        for (YMD ymd : ymdList){
            name.add(ymd.getName()+"-"+ymd.getName1()+"-"+ymd.getName2());
            money.add(ymd.getMoney());
            moneys += ymd.getMoney();
        }


        return new SplitDate("每日的销售情况",moneys,name,money);
    }

    @Override
    public int insertFinance(Finance finance) {
        String[] date = FinanceUtils.getDateStringUtils(finance.getTime());
        finance.setYear(date[0]);
        finance.setMonth(date[1]);
        finance.setDay(date[2]);
        finance.setHms(date[3]+":"+date[4]+":"+date[5]);
        financeMapper.insertFinance(finance);
        return 0;
    }

    @Override
    public int deleteFinanceById(Long id) {
        financeMapper.deleteFinanceById(id);
        return 0;
    }
}
